import axios from 'axios';
import { message } from 'antd';
import { getToken } from './auth'
const instance = axios.create({
    baseURL: 'https://zcapi.sdlysm.com/',
    timeout: 5000
    // headers: {'X-Custom-Header': 'foobar'}
})

// 请求拦截器
instance.interceptors.request.use(function (config) {
    Object.assign(config.data, { token: getToken() || '' })
    return config;
})
// 响应拦截器
instance.interceptors.response.use(function (response) {
    return response.data;
},function (error) {
    return Promise.reject(error)
})

export const requestGet = (url,data={}) => {
    return new Promise((resolve,reject) => { 
        instance.get(url,{params:data}).then(res => {
            resolve(res)
        }).catch(err => {
            reject(err)
        })
    })
}

export const requestPost = (url,data={}) => {
    return new Promise((resolve,reject) => { 
        instance.post(url,data).then(res => {
            if(res.code === 0){
                resolve(res.data)
            }else{
                message.error(res.msg)
                reject(res)
            }
        }).catch(err => {
            message.error('请求出错，请检查网络状况！')
            reject(err)
        })
    })
}